import ronin.graphics.GrimaceGamelet;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import ronin.actor2d.*;
import ronin.util.MessageDispatcher;
import ronin.util.Telegram;
import ronin.scene.QuadScene;
import ronin.ai.State;
import java.util.Vector;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
import ronin.graphics.RunAppletStub;
import ronin.util.EntityManager;
import com.dnsalias.java.timer.DurationTimer;

public class MessagingTest extends GrimaceGamelet {
	Rectangle2D sceneBounds = new Rectangle2D.Double(0, 0, 300, 300);
	QuadScene scene;
	
	TestGlobalState globalStateOne, globalStateTwo;
	
	StaticActorGroup actorGroup;
	
	StaticActor actorOne, actorTwo;
	
	public MessagingTest(){
		super(300, 300, true);
	}
    public void setupGame() {
    	actorGroup = new StaticActorGroup("bot.jpg");
    	actorGroup.init(this);
    	
    	actorOne = new StaticActor(actorGroup);
    	actorTwo = new StaticActor(actorGroup);
    	
    	registerEntity(actorOne);
    	registerEntity(actorTwo);
    	
    	actorOne.setPos(30, 30);
    	actorOne.update();
    	
    	actorTwo.setPos(200, 200);
    	actorTwo.update();
    	
    	globalStateOne = new TestGlobalState(actorOne);
    	globalStateTwo = new TestGlobalState(actorTwo);
    	
    	actorOne.setGlobalState(globalStateOne);
    	actorTwo.setGlobalState(globalStateTwo);
    	
    	scene = new QuadScene(this, 2, sceneBounds, sceneBounds, false);
    	scene.add(actorOne);
    	scene.add(actorTwo);
    	
    	gameletStarted = true;
    	MessageDispatcher.getInstance().newMessage(0, actorOne.uid, actorTwo.uid, 2, new Vector());
    	
    	System.out.println("ActorOnes ID is: " + new Integer(actorTwo.uid).toString());
    }

	public void runGame() {
		super.runGame();
		scene.update();
	}
	
	public void paintScene(Graphics2D g) {
		scene.paint(g);
	}
	
	
    /**
     * The main method.
     *    * @param args
     *                the arguments
     */
    public static void main(String args[]) {	
    String appletName = "Messaging Test";
	// Create the frame this applet will run in
	Frame appletFrame = new Frame(appletName);
	// This code lets you close the window
	WindowListener l = new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		System.exit(0);
	    }
	};
	appletFrame.addWindowListener(l);
	// The frame needs a layout manager, use the GridLayout to maximize
	// the applet size to the frame.
	appletFrame.setLayout(new GridLayout(1, 0));
	// Have to give the frame a size before it is visible
	appletFrame.setSize(300, 300);
	// Make the frame appear on the screen. You should make the frame appear
	// before you call the applet's init method. On some Java
	// implementations,
	// some of the graphics information is not available until there is a
	// frame.
	// If your applet uses certain graphics functions like getGraphics() in
	// the
	// init method, it may fail unless there is a frame already created and
	// showing.
	appletFrame.show();
	// Create an instance of the applet
	Applet myApplet = new MessagingTest();
	// Add the applet to the frame
	appletFrame.add(myApplet);
	// Now try to get an applet stub for this class.
	RunAppletStub stub = new RunAppletStub(appletFrame, myApplet,
		"RoNinEnGine " + appletName, "/D:/Users/R/Desktop/RoninEngine/bin/");
	myApplet.setStub(stub);
	// Initialize and start the applet
	myApplet.init();
	myApplet.start();

    }
}

class TestGlobalState extends State {
	String ownerID;
	DurationTimer msgTimer;
	
	public TestGlobalState(Actor2D owner){
		ownerID = new Integer(owner.uid).toString();
	}
	
	public void Enter(Actor2D owner){
		out("GlobalState has been Entered.");
		msgTimer = new DurationTimer(DurationTimer.getTicksPerSecond()*5);
		msgTimer.start();

	}
	
	public void Exit(Actor2D owner) {
		out("GlobalState has been Exited.");
	}
	
	public void Execute(Actor2D owner){
		
		if(msgTimer.getSecondsRemaining() == 0)
		{
			System.out.println("Its running");
			// send message
			MessageDispatcher.getInstance().newMessage(0, owner.uid, 1, 1, new Vector());
			// reset and start timer
			msgTimer.reset(10*DurationTimer.getTicksPerSecond());
			msgTimer.start();
		}
		else
		{
			
		}
	}
	
	public boolean onMessage(Actor2D owner, Telegram message){
		Integer rid, sid;
		rid = message.receiver;
		sid = owner.uid;
		String theSender = sid.toString();
		out("Bot" + rid.toString()  + " MessageReceived from " + theSender);
	//	MessageDispatcher.getInstance().newMessage(0.0, owner.uid, message.sender, 8, new Vector());
		return true;
	}
	
	private void out(String s){
		System.out.println("Bot" + ownerID + ": " + s);
	}
}
